<template>
  <div style="padding:15px">
    <vxe-toolbar id="costTypeId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button v-if="branch.level <1" @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
     <!-- <vxe-table-column >
        <vxe-table-column field="partyGroupName" title="所属组织机构" min-width="200" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.partyGroupCode" placeholder="请选择机构" clearable>
            <el-option
              v-for="item in partyGroup"
              :key="item.code"
              :label="item.groupName"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>-->
      <vxe-table-column>
        <vxe-table-column field="name" title="类型名称" min-width="100" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.name" placeholder="请输入类型名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="sysType" title="系统类型" min-width="100" :formatter="formatterSysType" sortable />
        <template v-slot:header>
          <dictElSelect ref="sysTypes" @querySysType="selectSysType" />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="branch.level <1" class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="branch.level <1" class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="500" height="280" resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="500" height="300" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelDetailFlag" title="详情" width="500" height="200" resize>
      <compDetail ref="detailRef" />
    </vxe-modal>

  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'

Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import compAdd from './components/costTypeAdd.vue'
import compEdit from './components/costTypeEdit.vue'
import compDetail from './components/costTypeDetail.vue'
import request from '@/utils/request'
import dictElSelect from '@/views/components/dictElSelect.vue'

export default {
  name: 'CostTypeList',
  components: {
    compAdd,
    compEdit,
    compDetail,
    dictElSelect
  },
  data() {
    return {
      message: '',
      loading: false,
      tableData: [],
      dictData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      modelDetailFlag: false,
      copy: false,
      partyGroup:[],

      listQuery: {
        code: '',
        name: '',
        sysType: '',
        partyGroupCode:'',

        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      dictType: {
        types: 'wmsSysType'
      },
      branch: this.$store.getters.branch
    }
  },
  created() {
    this.findDict()
    this.getPartyGroup()

  },
  methods: {
    //获取机构信息
    getPartyGroup() {
      request({
        url: '/arpa-basic-api/partyGroup/getPartyGroup',
        method: 'get'
      }).then(response => {
        this.partyGroup = response.data
        // this.$refs.partyGroupSelect.initData(this.partyGroup, 'setPartyGroupCode')
      }).then(response => {
        this.findList()
      })
    },

    // 选择系统类型
    selectSysType(value) {
      this.listQuery.sysType = value
    },
    // isCanalAudit系统类型
    formatterSysType({ cellValue }) {
      const sd = this.dictData.find(i => i.code === cellValue)
      return sd ? sd.name : ''
    },
    // 获取字典数据
    findDict() {
      request({
        url: '/arpa-basic-api/dict/listDict',
        method: 'get',
        params: this.dictType
      }).then(response => {
        this.dictData = response.data[0].dictList
        this.$refs.sysTypes.initDict(this.dictData, 'querySysType')
      })
    },
    findList() {
      this.loading = true
      return new Promise(resolve => {
        request({
          url: '/arpa-basic-api/costType',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          this.loading = false
          resolve();
        })
      })
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 添加方法
    funcAdd() {
      this.$refs.addRef.reset();    //重置

      this.$refs.addRef.$refs.sysTypes.initDict(this.dictData, 'changeSysType')
      this.$refs.addRef.partyGroup = this.partyGroup

      this.modelAddFlag = true
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.$refs.sysTypes.initDict(this.dictData, 'changeSysType')
      this.$refs.editRef.partyGroup = this.partyGroup

      this.$refs.editRef.getInfo(code)
      this.modelEditFlag = true
    },
    // 详情方法
    funcDetail(code) {
      this.$refs.detailRef.dictData = this.dictData
      this.$refs.detailRef.getInfo(code)
      this.modelDetailFlag = true
    },
    // 删除方法
    funcDelete(row) {
      this.$confirm('确认删除吗？').then(_ => {
        request({
          url: `/arpa-basic-api/costType`,
          method: 'delete',
          params: { codes: row }
        }).then(response => {
          this.$message({ message: response.msg, type: 'success' })
          this.findList()
        })
      }).catch(_ => {
      })
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
        case 'detail':
          this.modelDetailFlag = false
          break
        default:
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
